/*
 *
 * Created on 27.05.2013 10:24:11
 */
package org.opentcs.guing.components.dialogs;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.util.Collection;
import java.util.SortedSet;
import java.util.TreeSet;
import javax.swing.JPanel;
import org.opentcs.guing.model.elements.VehicleModel;

/**
 * Shows every vehicle available in the system in a panel.
 *
 * @author Philipp Seifert (Fraunhofer IML)
 */
public class VehiclesPanel
    extends JPanel {

  /**
   * The vehicle views sorted.
   */
  private final SortedSet<SingleVehicleView> vehicleViewsSorted
      = new TreeSet<>();

  /**
   * Creates a new instance.
   */
  public VehiclesPanel() {
    initComponents();
    setPreferredSize(new Dimension(0, 97));
    setMinimumSize(new Dimension(140, 120));
    panelVehicles.setLayout(new ModifiedFlowLayout(FlowLayout.LEFT, 10, 10));
  }

  /**
   * Initializes this panel with the current vehicles.
   *
   * @param vehicleModels The vehicle models.
   */
  public void setVehicleModels(Collection<VehicleModel> vehicleModels) {
    // Remove vehicles of the previous model from panel
    for (SingleVehicleView vehicleView : vehicleViewsSorted) {
      panelVehicles.remove(vehicleView);
    }

    // Remove vehicles of the previous model from list
    vehicleViewsSorted.clear();
    // Add vehicles of actual model to list
    for (VehicleModel vehicle : vehicleModels) {
      vehicleViewsSorted.add(new SingleVehicleView(vehicle));
    }

    // Add vehicles of actual model to panel, sorted by name
    for (SingleVehicleView vehicleView : vehicleViewsSorted) {
      panelVehicles.add(vehicleView);
    }

    panelVehicles.revalidate();
  }

  /**
   * Clears the vehicles in this panel.
   */
  public void clearVehicles() {
    for (SingleVehicleView vehicleView : vehicleViewsSorted) {
      panelVehicles.remove(vehicleView);
    }
    vehicleViewsSorted.clear();
  }

  @Override
  public void repaint() {
    super.repaint();

    if (vehicleViewsSorted != null) {
      for (SingleVehicleView view : vehicleViewsSorted) {
        view.repaint();
      }
    }
  }

  /**
   * This method is called from within the constructor to
   * initialize the form.
   * WARNING: Do NOT modify this code. The content of this method is
   * always regenerated by the Form Editor.
   */
  @SuppressWarnings("unchecked")
  // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  private void initComponents() {

    scrollPaneVehicles = new javax.swing.JScrollPane();
    panelVehicles = new javax.swing.JPanel();

    setName("VehiclesPanel"); // NOI18N
    setLayout(new java.awt.GridLayout(1, 0));

    scrollPaneVehicles.setViewportView(panelVehicles);

    add(scrollPaneVehicles);
  }// </editor-fold>//GEN-END:initComponents

  // Variables declaration - do not modify//GEN-BEGIN:variables
  private javax.swing.JPanel panelVehicles;
  private javax.swing.JScrollPane scrollPaneVehicles;
  // End of variables declaration//GEN-END:variables
}
